package cn.edu.nuc.fourth.controller;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.nuc.fourth.dto.PageInfo;
import cn.edu.nuc.fourth.model.Student;
import cn.edu.nuc.fourth.services.interfaces.FillingService;

@Controller
@RequestMapping("/student")
public class StudenController {

	@Autowired
	private FillingService service;

	@RequestMapping("/forstudentadd")
	public String forstudentadd(HttpSession session, Model model) {

		Student student = service.noAddStudent(-1);
		if (student != null) {
			model.addAttribute("student", student);
			if (student.getState() == 1) {

				return "student/noeditStudent";
			}
			return "student/oneStudent";
		}
		return "student/studentAdd";
	}

	@RequestMapping(value = "/studentadd", method = RequestMethod.POST)
	public String studentadd(@Valid @ModelAttribute("student") Student student, Errors errors) {

		if (errors.hasErrors()) {
			System.out.println("校验学生信息填报");
			return "student/studentAdd";
		}
		service.studentadd(student);
		return "OK";
	}

	@RequestMapping("/studentlist")
	public String studentlist(Integer userid, @RequestParam(name = "current", defaultValue = "1") Integer current,
			Model model) {

		PageInfo pageInfo = service.studentlist(-1, current);
		model.addAttribute("page", pageInfo);
		return "student/student";
	}

	@RequestMapping("/onestudent")
	public String onestudent(Integer studentid, Model model) {

		Student student = service.onestudent(studentid);
		model.addAttribute("student", student);
		model.addAttribute("s", "false");
		return "student/oneStudent";
	}

	@RequestMapping("/studentedit")
	public String studentedit(Student student, Model model) {

		Student s = service.studentedit(student);
		model.addAttribute("student", s);
		model.addAttribute("s", "true");
		return "student/oneStudent";
	}

	@RequestMapping("/noeditstudent")
	public String noeditstudent(Integer studentid, Integer state, Model model) {

		Student s = service.noeditstudent(studentid, state);
		model.addAttribute("student", s);
		return "student/noeditStudent";
	}

}
